package com.xinrui.attendanceweb.controller;

import com.github.pagehelper.PageInfo;
import com.xinrui.attendanceweb.common.R;
import com.xinrui.attendanceweb.pojo.Student;
import com.xinrui.attendanceweb.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("{pageCurrent}/{pageSize}")
    public R pageQueryStudent(@PathVariable Integer pageCurrent,
                              @PathVariable Integer pageSize){
        PageInfo<Student> studentPageInfo = studentService.pageQueryStudent(pageCurrent, pageSize);
        return new R(true,"查询学生列表成功",studentPageInfo);
    }

    @GetMapping("/{id}")
    public R findStudentById(@PathVariable Integer id){
        return new R(true,"查询学生列表成功",studentService.findStudentById(id));
    }

    @PutMapping
    public R updateStudent(@RequestBody Student student){
        if (studentService.update(student)){
            return new R(true,"修改学生成功!");
        }else{
            return new R(false,"修改学生失败!");
        }

    }

    @PostMapping
    public R saveStudent(@RequestBody Student student){
        if (studentService.save(student)){
            return new R(true,"添加学生成功!");
        }else{
            return new R(false,"添加学生失败!");
        }

    }

    @DeleteMapping("{id}")
    public R deleteStudent(@PathVariable Integer id){
        if (studentService.deleteStudent(id)){
            return new R(true,"删除学生成功!");
        }else{
            return new R(false,"删除学生失败!");
        }

    }

}
